/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managertelaCadastros;

import classes.Servico;
import classes.TipoServico;
import dao.DaoServico;
import java.util.List;
import telaCadastros.CadServico;

/**
 *
 * @author fumio
 */
public final class managerCadServico implements interfaceManager {

    private Servico servico;
    private CadServico cadServico;
    private DaoServico daoServico;

    public managerCadServico(Servico cservico) {
        cadServico = CadServico.getInstance(this);
        daoServico = new DaoServico();
        if (cservico == null) {
            servico = new Servico();
        } else {
            servico = cservico;
        }
        carregarTipoServico();
        cadServico.setVisible(true);
    }

    @Override
    public boolean salvar() {
        boolean resultado;
        try {
            if (servico == null) {
                servico = new Servico();
            }

            if (setData()) {
                daoServico.salvar(servico);
                
            }

            resultado = true;

        } catch (Exception e) {
            System.out.println(e.getMessage());
            resultado = false;
        }

        return resultado;
    }

    @Override
    public List carregar() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean setData() {
        boolean response;
        try {
            servico.setDescricao(cadServico.edtDescricaoServico.getText());
            servico.setIdtiposervico((TipoServico) cadServico.cmbTipoServico.getSelectedItem());
            servico.setPrecoServico(Double.parseDouble(cadServico.edtPreco.getText()));
           // servico.setTempo(Time.valueOf(cadServico.edtTempo.getText()));
            response = true;
        } catch (Exception e) {
            response = false;
        }
        return response;
    }

    @Override
    public void getData() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public void carregarTipoServico() {
        List<TipoServico> lista = daoServico.listaTipoServico();
        cadServico.cmbTipoServico.removeAllItems();
        TipoServico aux;
        for (int i = 0; i < lista.size(); i++) {
            aux = (TipoServico) lista.get(i);
            cadServico.cmbTipoServico.addItem(aux);
        }
    }

}
